package com.example.demo.aws3;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.HeadBucketRequest;

import java.util.List;

public class XskyS3Bucket {

    public AmazonS3 client = null;

    public XskyS3Bucket(AmazonS3 client) {
        this.client = client;
    }

    // 创建桶
    public Bucket CreatingBucket(String bucketName) {
        Bucket bucket = null;
        try {
            bucket = client.createBucket(bucketName);
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException when create\"" + bucketName + "\".");
            System.out.println("Error Message: " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code: " + ase.getErrorCode());
            System.out.println("Error Type: " + ase.getErrorType());
            System.out.println("Request ID: " + ase.getRequestId());
        }
        return bucket;
    }


    // 检查桶是否存在
    public Boolean HasBucket(String bucketName) {
        Boolean b = false;
        try {
            b = client.doesBucketExistV2(bucketName);
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException when check\"" + bucketName + "\".");
            System.out.println("Error Message: " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code: " + ase.getErrorCode());
            System.out.println("Error Type: " + ase.getErrorType());
            System.out.println("Request ID: " + ase.getRequestId());
            System.exit(1);
        }
        return b;
    }

    // 删除桶
    public void DeletingBucket(String bucketName) {
        try {
            client.deleteBucket(bucketName);
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException when delete\"" + bucketName + "\".");
            System.out.println("Error Message: " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code: " + ase.getErrorCode());
            System.out.println("Error Type: " + ase.getErrorType());
            System.out.println("Request ID: " + ase.getRequestId());
            System.exit(1);
        }
    }

    // 获取用户桶列表
    public List<Bucket> ListBuckets() {
        List<Bucket> bucketList = null;
        try {
            bucketList = client.listBuckets();
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException when listBuckets.");
            System.out.println("Error Message: " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code: " + ase.getErrorCode());
            System.out.println("Error Type: " + ase.getErrorType());
            System.out.println("Request ID: " + ase.getRequestId());
            System.exit(1);
        }
        return bucketList;
    }

    //检查是否有权限访问桶
    public void HeadBuckte(String bucketName) {
        HeadBucketRequest headBucketRequest = new HeadBucketRequest(bucketName);
        try {
            client.headBucket(headBucketRequest);
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException when head\"" + bucketName + "\".");
            System.out.println("Error Message: " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code: " + ase.getErrorCode());
            System.out.println("Error Type: " + ase.getErrorType());
            System.out.println("Request ID: " + ase.getRequestId());
            System.exit(1);
        }
    }
}
